package com.creatorblue.config;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

/**
 * mvc配置类
 * @author zhuq
 *
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer{

	@Value("${uploadPath}")
	private String uploadPath;
	@Value("${accessPath}")
	private String accessPath;
	/**
     * 配置使用session存储国际化相关配置信息
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
    	SessionLocaleResolver slr = new SessionLocaleResolver();
    	slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        return slr;
    }
    
	/**
	 * 注册拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/doLogin", "/login");
		registry.addInterceptor(new LocaleChangeInterceptor());
	}
	
	/**
	 * 注册静态资源映射
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler(accessPath + "**").addResourceLocations("file:" + uploadPath);
	}

}
